/**
 * 
 */
package com.aspire.android.heliar.pub.render;

import java.util.LinkedHashMap;
import java.util.Map;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Paint;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ImageSpan;
import android.util.Log;

import com.aspire.android.heliar.Heliar;
import com.aspire.android.heliar.pub.ContentNode;
import com.aspire.android.heliar.pub.ImageAttachment;
import com.aspire.android.heliar.pub.Pub;
import com.aspire.android.heliar.pub.PubAttachment;

/**
 * @author panglei
 * 
 */
public class OneFramePubRender extends PubRender {

	public static final String PUB_TITLE = "<u>%s</u>";
	public static final String PUB_TITLE_PARAGRAPH_SEP = "----------------------------------";
	public static final String PUB_PARAGRAPH_SEP = "<br/>";
	public static final String PUB_IMAGE = "pub_img";
	public static final String PUB_BACK_RETURN = "\n";
	public static final String PUB_CONTENT_NODE_SEP = "----------------------------------<br/>";

	public static final String PUB_PUB_TITLE = "<H2>%s</H2>";
	public static final String PUB_COLUMN_TITLE = "<H1>%s</H1>";

	public Map<String, Integer> outlines = new LinkedHashMap<String, Integer>();

	public OneFramePubRender(Paint paint) {
		super(paint);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.aspire.android.heliar.pub.render.PubRender#render(com.aspire.android
	 * .heliar.pub.Pub)
	 */
	@Override
	public SpannableStringBuilder render(Pub pub) {

		SpannableStringBuilder builder = new SpannableStringBuilder();

		builder.append(Html.fromHtml(String.format(PUB_PUB_TITLE,
				pub.getTitle())));
		builder.append(travelNodes(pub.getRootNode()));

		return builder;
	}

	private SpannableStringBuilder travelNodes(ContentNode node) {

		SpannableStringBuilder builder = new SpannableStringBuilder();

		if (node.getTitle() != null) {
			builder.append(Html.fromHtml(String.format(PUB_COLUMN_TITLE,
					node.getTitle())));
		}

		if (node.getContent() != null) {
			builder.append(Html.fromHtml(node.getContent().replaceAll(
					PUB_BACK_RETURN, PUB_PARAGRAPH_SEP)
					+ PUB_PARAGRAPH_SEP));
		}

		for (PubAttachment attachment : node.getAttachments()) {

			renderAttachment(attachment, builder);

			builder.append(Html.fromHtml(PUB_PARAGRAPH_SEP));
			builder.append(Html.fromHtml(PUB_PARAGRAPH_SEP));

		}

		if (node.getChildNodes().size() > 0) {
			for (ContentNode childNode : node.getChildNodes()) {
				builder.append(travelNodes(childNode));
			}
		}

		return builder;

	}

	private int renderAttachment(PubAttachment attachment,
			SpannableStringBuilder builder) {
		int attachmentHeight = 0;
		if (attachment instanceof ImageAttachment) {
			Log.d(Heliar.LOG_TAG, "add attachment");
			ImageAttachment imageAttachment = (ImageAttachment) attachment;

			Bitmap bitmap = null;
			if (imageAttachment.getBitmap() != null) {
				bitmap = imageAttachment.getBitmap();
			} else {
				bitmap = BitmapFactory.decodeFile(attachment.getFilepath());
			}

			// to get attachment height
			attachmentHeight = bitmap.getHeight();

			ImageSpan span = new ImageSpan(bitmap, ImageSpan.ALIGN_BASELINE);
			SpannableStringBuilder imagebuilder = new SpannableStringBuilder(
					PUB_IMAGE);

			imagebuilder.setSpan(span, 0, PUB_IMAGE.length(),
					Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			builder.append(imagebuilder);
		}

		return attachmentHeight;
	}

	// private SpannableStringBuilder renderAttachment(PubAttachment attachment,
	// Integer attachmentHeight) {
	// SpannableStringBuilder builder = new SpannableStringBuilder();
	// if (attachment instanceof ImageAttachment) {
	// Log.d(Heliar.LOG_TAG, "add attachment");
	// ImageAttachment imageAttachment = (ImageAttachment) attachment;
	//
	// Bitmap bitmap = null;
	// if (imageAttachment.getBitmap() != null) {
	// bitmap = imageAttachment.getBitmap();
	// } else {
	// bitmap = BitmapFactory.decodeFile(attachment
	// .getFilepath());
	// }
	//
	// // to get attachment height
	// attachmentHeight = bitmap.getHeight();
	//
	// ImageSpan span = new ImageSpan(bitmap, ImageSpan.ALIGN_BASELINE);
	// SpannableStringBuilder imagebuilder = new SpannableStringBuilder(
	// PUB_IMAGE);
	//
	// imagebuilder.setSpan(span, 0, PUB_IMAGE.length(),
	// Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
	// builder.append(imagebuilder);
	// }
	// return builder;
	// }

	@Override
	public Map<String, Integer> getPubOutlines() {

		return this.outlines;
	}

}
